/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "Hogshead.Common.h"
#include "../Managers/Messenger.h"
namespace Hogshead
{
	namespace Core
	{
		// forward declare
		class IMessageReceiver;
		class Messenger;

		/**
		* The IMessageSender interface is intended primarily for Directors and other objects which IMessageRecievers can access.
		* The IMessageSender maintains an internal list of IMessageReceivers to send each type of message to.  IMessageReceivers
		* can register themselves for an individual type of message.
		*/

		class IMessageSender
		{
		public:
			IMessageSender();
			virtual ~IMessageSender();
		protected:
			friend Messenger;

			/** 
			* Send the given message to the appropriate IMessageReceivers
			* The Messenger will release the message to the factory once it is handled
			**/
			template<typename T> void sendMessage(T* in_message)
			{
				int message_id = T::messageID();
				Vector<IMessageReceiver*>* receiver_list = NULL;
				if(_receiver_index->tryGetValue(message_id, receiver_list))
				{
					int receiver_count = receiver_list->size();
					IMessageReceiver* const*  _receiver_buffer = *receiver_list;
					for(int k = 0; k < receiver_count; k++)
						Messenger::sendMessage(in_message, _receiver_buffer[k]);
				}
				else
					Messenger::sendMessage(in_message, NULL);
			}
			/**
			* Forwards the given message to the registered IMessageRecievers.
			* The Messenger will not release to the factory this message once it has been handled.
			* The IMessageSender must handle the memory management of the forwarded message.
			*/
			template<typename T> void forwardMessage(T* in_message)
			{
				int message_id = T::messageID();
				Vector<IMessageReceiver*>* receiver_list = NULL;
				if(_receiver_index->tryGetValue(message_id, receiver_list))
				{
					int receiver_count = receiver_list->size();
					IMessageReceiver* const*  _receiver_buffer = *receiver_list;
					for(int k = 0; k < receiver_count; k++)
						Messenger::forwardMessage(in_message, _receiver_buffer[k]);
				}
			}

			// maps message ids to receiver lists
			HashTable<int, Vector<IMessageReceiver*>*>* _receiver_index;
			int _sender_id;
			static int _sender_counter;
		};
	}
}